 
pygame.init()
WHITE = (255,255,255)
YELLOW = (255,255,0)
BLUE = (0,0,255)
BLACK = (0,0,0)

if olpcgames.ACTIVITY:
    size = olpvgames.ACTIVITY.game_size
else:
    size = RES
screen = pygame.display.set_mode( size )
font = pygame.font.Font(None, SIZE)

renderedChars = {}
def getChar(char):
    if char not in renderedChars:
        renderedChars[char] = font.render(char, True, WHITE)
    return renderedChars[char]

def main():
    def insertar(linea, color=WHITE):
        screen.blit(screen,(0,-SIZE),screen.get_rect(height=screen.get_rect().height-SIZE))
        lineaSurf = font.render(linea, True, color)
        screen.fill(BLACK, screen.get_rect(height=SIZE, bottom=screen.get_rect().bottom-SIZE))
        screen.blit(lineaSurf, lineaSurf.get_rect(bottom=screen.get_rect().bottom-SIZE))

    def limpiar():
        screen.fill(BLUE, screen.get_rect(height=SIZE, bottom=screen.get_rect().bottom))

    running = True
    clock = pygame.time.Clock()

    linea = []
    x = 0

    tick = 0
    limpiar()
    while running:
        for event in pygame.event.get():
            if event.type == KEYDOWN:
                if event.key == K_RETURN:
                    limpiar()
                    texto = "".join(linea)
                    insertar(texto)
                    # enviar(texto)
                    linea = []
                    x = 0
                else:
                    char = event.unicode
                    if char is not None:
                        charSurface = getChar(char)
                    screen.blit(charSurface, charSurface.get_rect(bottom=screen.get_rect().bottom, left=x))
                    x += charSurface.get_rect().width
                    linea.append(char)
            if event.type == olpcgames.CONNECT:
                print 'conectado'
                def on_buddy(buddy, event=event):
                    print 'on_buddy'
                mesh.lookup_buddy(event.handle, on_buddy)
            if event.type == olpcgames.PARTICIPANT_ADD:
                print 'se agrego uno'
            if event.type == olpcgames.PARTICIPANT_REMOVE:
                print 'se elimino uno'

        tick += 1
        if tick % 100 == 0:
            insertar("dale boludo....", YELLOW)

        pygame.display.flip()
        clock.tick(30)

if __name__ == "__main__":
    main()
